package com.future.hair.server.custom.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.hair.server.config.jjwt.JwtHelper;
import com.future.hair.server.config.result.CommonResult;
import com.future.hair.server.custom.Vo.AppraiseVo;
import com.future.hair.server.custom.Vo.CommentVo;
import com.future.hair.server.entity.BarberAppraise;
import com.future.hair.server.entity.User;
import com.future.hair.server.entity.UserOrder;
import com.future.hair.server.entity.WorksComment;
import com.future.hair.server.mapper.BarberAppraiseMapper;
import com.future.hair.server.mapper.UserMapper;
import com.future.hair.server.mapper.UserOrderMapper;
import com.future.hair.server.mapper.WorksCommentMapper;
import com.future.hair.server.model.PageModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("//hair/appraiseVo")
@Api(tags = "appraiseVo管理接口")
public class AppraiseVoController {

    @Autowired
    private BarberAppraiseMapper mapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserOrderMapper orderMapper;

    @ApiOperation(value = "查询 AppraiseVo 列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult<List<AppraiseVo>> worksVoList(@RequestBody PageModel data) {
        List<AppraiseVo> appraiseVoList = new ArrayList<AppraiseVo>();
        List<BarberAppraise> barberAppraiseList = barberAppraiseList(data);
        for (BarberAppraise info:
                barberAppraiseList) {
            appraiseVoList.add(getAppraiseVo(info));
        }
        return CommonResult.data(appraiseVoList);
    }


    public List<BarberAppraise> barberAppraiseList(PageModel data) {
        QueryWrapper<BarberAppraise> appraiseWrapper = new QueryWrapper<BarberAppraise>();
        appraiseWrapper.eq("barber_id",data.getId());
        if (null!=data.getType()){
            appraiseWrapper.eq("type",data.getType());
        }
        return mapper.selectPage(new Page<>(data.getSize(), data.getCurrent()), appraiseWrapper).getRecords();
    }

    @ApiOperation(value = "修改评价")
    @RequestMapping(value = "/appraiseType", method = RequestMethod.POST)
    public CommonResult<Integer> appraiseType(@RequestBody BarberAppraise data, @RequestHeader String token) {
        Integer id = data.getId();
        if (null==data.getId() || null==data.getBarberId() || null == data.getType()){
            return CommonResult.failed("参数id,barberId,type不能为空");
        }
        if (data.getType()<1 || data.getType()>3){
            return CommonResult.failed("参数type范围不超过[1-3]");
        }
        QueryWrapper<BarberAppraise> appraiseWrapper = new QueryWrapper<BarberAppraise>();
        appraiseWrapper.eq("id", data.getId());
        appraiseWrapper.eq("barber_id", data.getBarberId());
        appraiseWrapper.eq("user_id", JwtHelper.getByUserId(token));
        BarberAppraise appraise = new BarberAppraise();
        appraise.setType(data.getType());
        Integer result = mapper.update(appraise,appraiseWrapper);
        if (result==1){
            return CommonResult.data(result);
        }else {
            return CommonResult.failed("更新失败");
        }
    }

    public AppraiseVo getAppraiseVo(BarberAppraise data){
        if (null==data){
            return null;
        }

        AppraiseVo appraiseVo = new AppraiseVo();
        appraiseVo.setId(data.getId());
        appraiseVo.setUserId(data.getUserId());
        appraiseVo.setBarberId(data.getBarberId());
        appraiseVo.setOrderId(data.getOrderId());
        appraiseVo.setCreateTime(data.getCreateTime());
        appraiseVo.setType(data.getType());

        UserOrder order = orderMapper.selectById(data.getUserId());
        appraiseVo.setReserves(order.getReserves());

        User user = userMapper.selectById(data.getUserId());
        appraiseVo.setAvatar(user.getAvatar());
        appraiseVo.setUserName(user.getUsername());

        return appraiseVo;
    }
}
